package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryBrandService categoryBrandService;

    /*
     * 根据品牌id，查询分类集合
     * */
    @Override
    public List<CategoryDTO> findCategoryByBrandId(Long id) {

        // 查品牌分类中间表
        List<CategoryBrand> categoryBrands = categoryBrandService.query().eq("brand_id", id).list();

            if (CollectionUtils.isEmpty(categoryBrands)){
                return Collections.emptyList();
            }
        // 查分类表
        List<Long> categoryIds = categoryBrands.stream().map(CategoryBrand::getCategoryId).collect(Collectors.toList());
        List<Category> categories = listByIds(categoryIds);
        return CategoryDTO.convertEntityList(categories);
    }
}
